package cn.zucc.edu.madq.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zucc.edu.madq.pojo.Message;
import cn.zucc.edu.madq.pojo.User;
import cn.zucc.edu.madq.service.MessageService;

@Controller
@RequestMapping("message")
public class MessageController {
	@Autowired
	private MessageService messageService;
	private static Logger logger = Logger.getLogger(MessageController.class);

	@RequestMapping("getMessageNum")
	@ResponseBody
	public JSONPObject getMessageNum(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------start------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		int count = 0;
		try {
			User user = (User) request.getSession().getAttribute("user");
			count = messageService.getMessageNum(user.getUserId());
		} catch (Exception e) {
			count = -1;
			logger.error(e.getMessage(), e);
		}
		logger.info("------end------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		return new JSONPObject(jsonpCallback, count);
	}
	@RequestMapping("getMyMessage")
	@ResponseBody
	public JSONPObject getMyMessage(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------start------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		int count = 0;
		try {
			count = 1;
			User user = (User) request.getSession().getAttribute("user");
			List<Message> m = messageService.getAllMessage(user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, m);
		} catch (Exception e) {
			count = -1;
			logger.error(e.getMessage(), e);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}
		
	}
	@RequestMapping("delMessage")
	@ResponseBody
	public JSONPObject delMessage(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("messageId") int messageId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------start------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		int count = 0;
		try {
			User user = (User) request.getSession().getAttribute("user");
			Message message = messageService.getMessageById(messageId);
			if(message.getMessageTo().intValue() == user.getUserId().intValue()){
				message.setMessageIsRead(1);
				count = messageService.readMessage(message);
			}
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.error(e.getMessage(), e);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}
		
	}
	@RequestMapping("addMessage")
	@ResponseBody
	public JSONPObject addMessage(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("messageTo") int messageTo,
			@RequestParam("messageContent") String messageContent,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------start------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		int count = 0;
		try {
			User user = (User) request.getSession().getAttribute("user");
			Message m = new Message();
			m.setMessageCreatDate(new Date());
			m.setMessageContent(messageContent);
			m.setMessageFrom(user.getUserId());
			m.setMessageTo(messageTo);
			m.setMessageIsRead(0);	
			count = messageService.addMessage(m);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.error(e.getMessage(), e);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}
		
	}
}
